<?php
ob_start();
include_once('connection/connection.php');
if(isset($_POST['submit']))
{
		//$q2_id = $_SESSION['q2_id'];
		//echo $q2_id;
		$id=$_REQUEST['Id'];
		$q2_id=$_REQUEST['frmId'];
		list($habit_id,$hname)=explode(",",$_POST['habitation_name']);
		$type_mh_oh=$_POST['type_mh_oh'];
		$location=$_POST['location'];
		$latitude=$_POST['latitude'];
		$longitude=$_POST['longitude'];
		$function_status=$_POST['function_status'];
		
		if($function_status=="NOT FUNCTIONAL"){
			$_POST['opration_status']=$_POST['quality_status']=$_POST['yield']=$_POST['seasonal_flunctuation']=$_POST['fluctuation_min']=$_POST['fluctuation_max']=$_POST['para_ph']=$_POST['para_tds']=$_POST['para_cl']=$_POST['para_f']=$_POST['para_no3']=$_POST['firstinput']="";
			}
		
		$opration_status=$_POST['opration_status'];
		$quality_status=$_POST['quality_status'];
		$yield=$_POST['yield'];
		$seasonal_flunctuation=$_POST['seasonal_flunctuation'];
		$water_table_fluctuation_min=$_POST['fluctuation_min'];
		$water_table_fluctuation_max=$_POST['fluctuation_max'];
		$para_ph=$_POST['para_ph'];
		$para_tds=$_POST['para_tds'];
		$para_cl=$_POST['para_cl'];
		$para_f=$_POST['para_f'];
		$para_no3=$_POST['para_no3'];
		$para_testing_date=$_POST['firstinput'];
		$repairable_status=$_POST['repairable_status'];
		$remarks=$_POST['remarks'];
		$hp_sp =$_POST['hp_sp'];
			
			
			$qr="INSERT INTO tbl_dtls_q2_hp_status(q2_id, habit_code , habitation_name, type_mh_oh, hp_sp , location, latitude, longitude, functional_status, operational_status, quality_status, yield, seasonal_fluctuation, water_table_min, water_table_max, para_ph, para_tds, para_cl, para_f, para_no3,para_testing_date,last_modify, repairable_status, remark) VALUES ('$q2_id', '$habit_id' , '$hname', '$type_mh_oh', '$hp_sp' ,'$location', '$latitude' , '$longitude' ,'$function_status', '$opration_status', '$quality_status', '$yield', '$seasonal_flunctuation', '$water_table_fluctuation_min', '$water_table_fluctuation_max','$para_ph','$para_tds','$para_cl','$para_f','$para_no3','$para_testing_date', NOW(),'$repairable_status', '$remarks')"; 
			
			$res=mysql_query($qr);
			
			$qr1="INSERT INTO tbl_dtls_q2_hp_status_temp(q2_id, habit_code , habitation_name, type_mh_oh, hp_sp , location, latitude, longitude, functional_status, operational_status, quality_status, yield, seasonal_fluctuation, water_table_min, water_table_max, para_ph, para_tds, para_cl, para_f, para_no3,para_testing_date,last_modify, repairable_status, remark) VALUES ('$q2_id', '$habit_id' , '$hname', '$type_mh_oh', '$hp_sp' ,'$location', '$latitude' , '$longitude' ,'$function_status', '$opration_status', '$quality_status', '$yield', '$seasonal_flunctuation', '$water_table_fluctuation_min', '$water_table_fluctuation_max','$para_ph','$para_tds','$para_cl','$para_f','$para_no3','$para_testing_date', NOW(),'$repairable_status', '$remarks')";
			
			$res1=mysql_query($qr1);
			//echo $res;
			/*
			if($hp_sp=="HP") 
			{
				mysql_query("UPDATE tbl_dtls_q2_habit set hp_no=(hp_no + 1) where q2_id='$q2_id' and habit_name='$hname'");
			}
			if($hp_sp=="SP")
			{
				mysql_query("UPDATE tbl_dtls_q2_habit set sp_no=(sp_no + 1) where q2_id='$q2_id' and habit_name='$hname'");
			}
	*/
	//function for count total no. of HP and SP and get value to calling function	
	function countSource($hcode,$hp_sp)
	{
		$qryCount="select count(*) as no from tbl_dtls_q2_hp_status where habit_code='$hcode' and hp_sp='$hp_sp'";
		$recCount=mysql_query($qryCount);
		$rowCount=mysql_fetch_array($recCount);
		return $rowCount[0];
	}
	//Update thevalue of HP and SP in HABITATION DETAIL PAGE
	$qryHabit="select * from tbl_dtls_q2_habit where q2_id='$q2_id' group by habit_code";
	$recHabit=mysql_query($qryHabit);
	while($rowHabit=mysql_fetch_array($recHabit))
	{
		$totalHP=countSource("$rowHabit[habit_code]","HP");
		$totalSP=countSource("$rowHabit[habit_code]","SP");
	
		$a="update tbl_dtls_q2_habit set hp_no='$totalHP',sp_no='$totalSP' where habit_code='$rowHabit[habit_code]'";
		mysql_query($a);
	}
	
	$qryHistory="insert into tbl_new_user_history(user_id,form_id,action_id,ip_address,modify_date,user_type,user_name) values('$_POST[loginId]','$q2_id','109','$_SERVER[REMOTE_ADDR]',NOW(),'$_POST[userType]','$_POST[name]')";
mysql_query($qryHistory);
	
			if($res && $res1)
			{
				header('Location: q2.php');
				//echo "<script>window.location.href(q2.hp);</script>";
			}
			
		}		
		
?>
